#include "Lora.h"

uint8_t BufferSize = BUFFER_SIZE;
uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT;
uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT;

PacketStatus_t packetStatus;

ModulationParams_t light_modulationParams={
    .PacketType                  = PACKET_TYPE_LORA,
    .Params.LoRa.SpreadingFactor = LORA_SF5,
    .Params.LoRa.Bandwidth       = LORA_BW_0800,//LORA_BW_1600,
    .Params.LoRa.CodingRate      = LORA_CR_LI_4_7
};
ModulationParams_t boot_modulationParams={
    .PacketType                  = PACKET_TYPE_LORA,
    .Params.LoRa.SpreadingFactor = LORA_SF5,
    .Params.LoRa.Bandwidth       = LORA_BW_1600,
    .Params.LoRa.CodingRate      = LORA_CR_LI_4_7
};

PacketParams_t packetParams={
    .PacketType                      = PACKET_TYPE_LORA,
    .Params.LoRa.PreambleLength      = 12,
    .Params.LoRa.HeaderType          = LORA_PACKET_VARIABLE_LENGTH,
    .Params.LoRa.PayloadLength       = BUFFER_SIZE,
    .Params.LoRa.CrcMode             = LORA_CRC_ON,
    .Params.LoRa.InvertIQ            = LORA_IQ_NORMAL
};

RadioCallbacks_t Callbacks =
{
    &OnTxDone,        // txDone
    &OnRxDone,        // rxDone
    NULL,             // syncWordDone
    NULL,             // headerDone
    &OnTxTimeout,     // txTimeout
    &OnRxTimeout,     // rxTimeout
    &OnRxError,       // rxError
    NULL,             // rangingDone
    NULL,             // cadDone
};

bool isMaster = true;
void sx1280_int(void)
{
    uint32_t pclk_freq=0;

    PERIPHERAL_ENABLE(SPI, 0);
    DMAC_DisableSyncRequest(SPI_TX_DMA_REQ(SPI0));
    DMAC_DisableSyncRequest(SPI_RX_DMA_REQ(SPI0));
    pclk_freq = SYS_GetPclkFreq();
    SPI_SclkDivTypeDef spi_sclk_div = pclk_freq > 240 ? SPI_CTRL_SCLK_DIV16 : pclk_freq > 120 ? SPI_CTRL_SCLK_DIV8 : SPI_CTRL_SCLK_DIV16;
    SPI_Init(SPI0, spi_sclk_div);

    UTIL_IdleUs(500000);                //500ms  let DC/DC power ramp up
    Radio.Init( &Callbacks );
    Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
    Radio.SetStandby( STDBY_RC );
    Radio.SetPacketType( boot_modulationParams.PacketType );             //包类型 
    Radio.SetModulationParams( &boot_modulationParams );                 //调制参数
    Radio.WriteRegister(0x925,0x1E);
    Radio.SetPacketParams( &packetParams );
    Radio.SetRfFrequency( RF_FREQUENCY );
    Radio.SetBufferBaseAddresses( 0x00, 0x00 );                           //接收
    Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );
    SX1280SetInterruptMode();
}

void load_light_param(uint8_t length, uint8_t freq)
{   
    uint32_t freqency=0;
    freqency = (2400+freq)*1000000;

    packetParams.Params.LoRa.PayloadLength = length;
    Radio.SetStandby( STDBY_RC );
    Radio.SetPacketType( light_modulationParams.PacketType );             //包类型 
    Radio.SetModulationParams( &light_modulationParams );                 //调制参数
    Radio.WriteRegister(0x925,0x1E);
    Radio.SetPacketParams( &packetParams );
    Radio.SetRfFrequency( freqency );
    Radio.SetBufferBaseAddresses( 0x00, 0x00 );                           //接收
    Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );
}

void load_boot_param(uint8_t length)
{   
    packetParams.Params.LoRa.PayloadLength = length;
    Radio.SetStandby( STDBY_RC );
    Radio.SetPacketType( boot_modulationParams.PacketType );             //包类型 
    Radio.SetModulationParams( &boot_modulationParams );                 //调制参数
    Radio.WriteRegister(0x925,0x1E);
    Radio.SetPacketParams( &packetParams );
    Radio.SetRfFrequency( RF_FREQUENCY );
    Radio.SetBufferBaseAddresses( 0x00, 0x00 );                           //接收
    Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );
}
